package org.mentalsmash.tazio.evaluation;

import java.util.ArrayList;
import java.util.List;

public class JMXHarvester {

    private static int completed = 0;
    
    /**
         * @param args
         */
    public static void main(String[] args) {
	final String testId = args[0];
	final int numRM = Integer.parseInt(args[1]);
	final int numWRM = Integer.parseInt(args[2]);

	String servicePrefix = "service:jmx:rmi:///jndi/rmi://192.168.70.";
	String servicePostfix = ":9999/jmxrmi";

	final Object lock = new Object();
	
	int wrmidx = 0;
	
	for (int i = 166; i <= 172 && wrmidx < numWRM; i++) {
	    if(i == 169 || i == 170) {
		continue;
	    }
	    wrmidx++;
	    final String uri = servicePrefix + i + servicePostfix;
	    final int idx = i;
	    
	    Runnable task = new Runnable() {
	    
	        public void run() {
	            try {
			JMXDataRetriever retriever = new JMXDataRetriever(idx+"", testId, uri);

			retriever.dump();
			
			synchronized (lock) {
			    completed++;
			    lock.notifyAll();
			}
		    } catch (Exception e) {
			e.printStackTrace();
		    }
	    
	        }
	    
	    };
	    
	    new Thread(task).start();
	}
	
	synchronized (lock) {
	    while(completed < numWRM) {
		try {
		    lock.wait();
		} catch (InterruptedException e) {
		    e.printStackTrace();
		}
	    }
	}
	
	int rmIdx = 0;
	for (int i = 164; i <= 165 && rmIdx < numRM; i++) {
	    final String uri = servicePrefix + i + servicePostfix;
	    final int idx = i;
	    rmIdx++;
	    Runnable task = new Runnable() {
	    
	        public void run() {
	            try {
			JMXDataRetriever retriever = new JMXDataRetriever(idx+"", testId, uri);

			retriever.dump();
			
			synchronized (lock) {
			    completed++;
			    lock.notifyAll();
			}
		    } catch (Exception e) {
			e.printStackTrace();
		    }
	    
	        }
	    
	    };
	    
	    new Thread(task).start();
	}
	
	synchronized (lock) {
	    while(completed < numRM+numWRM) {
		try {
		    lock.wait();
		} catch (InterruptedException e) {
		    e.printStackTrace();
		}
	    }
	}
	
	System.out.println("YO MAN, I'm done!");
	
    }

}
